/**
 * Copyright (C) Skywares Information Technology, LTD. 
 * All Rights Reserved.
 *
 * BizServiceAspect.java created on 2014-9-18 1:52:42 by justin.jiang 
 */
package com.mvc.server.aspect;

import java.util.Objects;
import java.util.Stack;

import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.annotation.After;
import org.aspectj.lang.annotation.AfterThrowing;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.stereotype.Component;


/**
 * <pre>
 * Description
 * 服务调用切面，设置数据源及打印调用参数
 *
 * @author justin.jiang
 * @since 1.0
 *
 * Change History
 * Date            Modifier        DESC.
 * 2014-9-18      justin.jiang         Initial version.
 * </pre>
 */
@Aspect
@Component
public class BizServiceAspect
{
    private ThreadLocal<Stack<String>> dsStack = new ThreadLocal<Stack<String>>();
    
    @Pointcut("execution(* com.mvc..service.*.*(..))")
    public void matchCondition() {}
    
    @Before("matchCondition()")
    public void doBefore(JoinPoint jp) 
    {
    	if (Objects.isNull(dsStack.get())) {
    		dsStack.set(new Stack<String>());
		}
    	
    	dsStack.get().push(jp.getTarget().getClass().getName());
    } 
    
    @Around("matchCondition()")
    public Object doAround(ProceedingJoinPoint pjp) throws Throwable 
    {
        Object retVal = pjp.proceed();
        return retVal;  
    }  

    @After("matchCondition()")
    public void doAfter(JoinPoint jp) 
    {  
    	if (Objects.nonNull(dsStack.get()) && !dsStack.get().isEmpty()) {
    		dsStack.get().pop();
		}
    }  
    
    @AfterThrowing(value = "matchCondition()", throwing="ex")
    public void doThrowing(JoinPoint jp, Throwable ex)
    {
    }
}
